package com.giscloud.amp.controller.auth;


import com.giscloud.amp.controller.BaseController;
import com.giscloud.amp.entity.ApKeyRuleRelation;
import com.giscloud.amp.entity.ApRuleMatch;
import com.giscloud.amp.service.auth.ApKeyRuleRelationService;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@RestController
public class ApKeyRuleRelationController extends BaseController {

    @Autowired
    private ApKeyRuleRelationService apKeyRuleRelationRemoteClient;


    public static final String rule_relation_user_key_prefix = "amp:matchrule:user";

    @Value("${gxd.address.matchrule.keeptime:12600}")
    private String matchrule_keeptime;

    /**
     * 新增（启用）匹配规则
     * @param record
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/insert", method = RequestMethod.POST)
    public Result insertSelective(@RequestBody ApKeyRuleRelation record){
        Result result = new Result();
        if(record == null || StringUtils.isBlank(record.getCreatedStaffId()) || StringUtils.isBlank(record.getKeyCode())
                || record.getRuleId() == null || record.getCityName()== null){
            result.setMsg("启用规则失败！必填参数缺失");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.insertSelective(record);
        result.setMsg("启用规则成功");
        result.setCode(0);
        result.setSuccess(true);
        return result;
    }

    /**
     * 删除（禁用）匹配规则
     * @param record
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/delete")
    public Result deleteByPrimaryKey(@RequestBody ApKeyRuleRelation record){
        Result result = new Result();
        if(record == null || StringUtils.isBlank(record.getCreatedStaffId()) || StringUtils.isBlank(record.getKeyCode())
                || record.getRelationId() == null){
            result.setMsg("禁用规则失败！必填参数缺失");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.deleteByPrimaryKey(record);
        result.setMsg("禁用规则成功");
        result.setCode(0);
        result.setSuccess(true);
        return result;
    }

    /**
     * 查询匹配规则列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/selectByExample", method = RequestMethod.POST)
    public Map<String,Object> selectByExample(@RequestBody Map<String, Object> params){

        Map<String,Object> result = new HashMap<String,Object>();
        if(params.get("keyCode") == null || params.get("userId") == null || params.get("userName") == null||params.get("cityName") == null ||
            "".equals(params.get("cityName"))|| "".equals(params.get("userId")) || "".equals(params.get("userName"))|| "".equals(params.get("cityName"))){
            result.put("message","查询列表失败！必填参数缺失");
            result.put("success",false);
            return result;
        }
        PageInfo pageInfo = apKeyRuleRelationRemoteClient.selectByExample(params);
        result.put("message","处理成功！");
        result.put("success",true);
        result.put("total",pageInfo.getTotal());
        result.put("rows",pageInfo.getList());
        return result;
    }

    /**
     * 查询城市列表
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/getCityList", method = RequestMethod.POST)
    public Result getCityList(@RequestBody Map<String, Object> params){
        Result result = new Result();
        List<ApKeyRuleRelation> list = apKeyRuleRelationRemoteClient.getCityList(params);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        result.setData(map);
        return result;
    }

    /**
     * 修改匹配规则排序
     * @param record
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/updateByPrimaryKeySelective")
    public Result updateByPrimaryKeySelective(@RequestBody ApKeyRuleRelation record){
        Result result = new Result();
        if(record == null || StringUtils.isBlank(record.getCreatedStaffId()) || StringUtils.isBlank(record.getKeyCode())
                || record.getRelationId() == null || record.getDispOrder() == null){
            result.setMsg("修改排序失败！必填参数缺失");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.updateByPrimaryKeySelective(record);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        return result;
    }

    /**
     * 向上或向下调整排序
     * @param params
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/upOrDownOrder",method = RequestMethod.POST)
    public Result upOrDownOrder(@RequestBody Map<String, Object> params){
        Result result = new Result();
        if(params.get("keyCode") == null || params.get("cityName") == null || params.get("userId") == null ||
                params.get("relationId1") == null || params.get("dispOrder1") == null
                || params.get("relationId2") == null||params.get("relationId2") == null){
            result.setMsg("处理失败！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.upOrDownOrder(params);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        return result;
    }

    @RequestMapping(value = "/amp/static/apKeyRuleRelation/getToken",method = RequestMethod.POST)
    public Result getToken(@RequestBody Map<String, Object> params){
        Result result = new Result();
        if(params.get("userName") == null || "".equals(params.get("userName"))){
            result.setMsg("用户名为空！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }

        String userName = String.valueOf(params.get("userName"));
//        Map<String, Object> claims =new HashMap<String, Object>();
//        claims.put("username",userName);
//        claims.put("isRememberMe",false);
//        String token = JwtTokenUtils.createToken(claims);
        String token = String.valueOf(System.currentTimeMillis());
       // redisUtils.set(rule_relation_user_key_prefix+userName,token,Long.valueOf(matchrule_keeptime));
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        result.setData(map);
        return result;
    }


    @RequestMapping(value = "/amp/static/apKeyRuleRelation/selectByLogExample", method = RequestMethod.POST)
    public PageInfo selectByLogExample(@RequestBody Map<String,Object> params) {
        return apKeyRuleRelationRemoteClient.selectLogByExample(params);
    }

    /**
     * 查询匹配规则列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/selectRuleList", method = RequestMethod.POST)
    public PageInfo selectRuleList(@RequestBody Map<String, Object> params){
        return apKeyRuleRelationRemoteClient.selectRuleList(params);
    }

    /**
     * 批量启用匹配规则
     * @param params
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/batchEnableRule",method = RequestMethod.POST)
    public Result batchEnableRule(@RequestBody Map<String, Object> params){
        Result result = new Result();
        if(params.get("keyCode") == null || params.get("cityId") == null || params.get("userId") == null || params.get("userName") == null || params.get("ruleId") == null ){
            result.setMsg("必填参数缺失！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.batchEnableRule(params);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        return result;
    }

    /**
     * 批量禁用匹配规则
     * @param params
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/batchDisableRule",method = RequestMethod.POST)
    public Result batchDisableRule(@RequestBody Map<String, Object> params){
        Result result = new Result();
        if(params.get("keyCode") == null || params.get("cityId") == null || params.get("userId") == null || params.get("userName") == null || params.get("ruleId") == null ){
            result.setMsg("必填参数缺失！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.batchDisableRule(params);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        return result;
    }

    /**
     * 查询指定规则下的城市列表
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/getCityListByRule",method = RequestMethod.POST)
    public Result getCityListByRule(@RequestBody Map<String, Object> params){
        Result result = new Result();
        if(params.get("keyCode") == null || params.get("ruleId") == null ){
            result.setMsg("必填参数缺失！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        List<ApKeyRuleRelation> list = apKeyRuleRelationRemoteClient.getCityListByRule(params);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        result.setData(list);
        return result;
    }

    /**
     * 查询规则详情
     * @param apRuleMatch
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/selectApRule",method = RequestMethod.POST)
    public Result selectApRule(@RequestBody ApRuleMatch apRuleMatch){
        Result result = new Result();
        if(apRuleMatch.getRuleId() == null){
            result.setMsg("必填参数缺失！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        ApRuleMatch resultAp = apKeyRuleRelationRemoteClient.selectApRule(apRuleMatch);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        result.setData(resultAp);
        return result;
    }

    /**
     * 修改规则详情
     * @param apRuleMatch
     * @return
     */
    @RequestMapping(value = "/amp/static/apKeyRuleRelation/updateApRule",method = RequestMethod.POST)
    public Result  updateApRule(@RequestBody ApRuleMatch apRuleMatch){
        Result result = new Result();
        if(apRuleMatch.getRuleId() == null || apRuleMatch.getRemark() == null){
            result.setMsg("必填参数缺失！");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        apKeyRuleRelationRemoteClient.updateApRule(apRuleMatch);
        result.setCode(0);
        result.setSuccess(true);
        result.setMsg("处理成功！");
        return result;
    }
}
